#ifndef _T2AUTILITIES_H
#define _T2AUTILITIES_H

#ifndef T2AUTILITIES_FLOAT_TYPE
#pragma message("T2AUtilities: Using float for T2AUTILITIES_FLOAT_TYPE")
#define T2AUTILITIES_FLOAT_TYPE float
#endif

static T2AUTILITIES_FLOAT_TYPE sqrarg;
#define SQR(a) ((sqrarg=(a)) == 0.0f ? 0.0f : sqrarg*sqrarg)
// TODO: enclose macros in {} for operations within if, else, and other blocks
#ifndef VEC3_OP
#define VEC3_OP(a,op,b) \
	(a)[0] op (b)[0]; \
	(a)[1] op (b)[1]; \
	(a)[2] op (b)[2]
#endif

#ifndef VEC3_EXPAND
#define	VEC3_EXPAND(v) \
	(v)[0],(v)[1],(v)[2]
#endif

#ifndef VEC3_EXPAND_OP
#define	VEC3_EXPAND_OP(v,op,a,b,c) \
	(v)[0] op (a);(v)[1] op (b);(v)[2] op (c)
#endif

#include <math.h>

static int roundftoi(float x)
{
	double intpart;
	double frac;
	frac = modf((double)x,&intpart);

	if(frac>=0.5) return (int)ceil(x);
	if(frac>=0.0) return (int)intpart;

	if(frac<=-0.5f) return (int)floor(x);
	return (int)intpart;
}

/*
 * vector stuff
 */

#ifndef V3opV3opV3
#define V3opV3opV3(a, op1, b, op2, c) \
	(a)[0] op1 ((b)[0] op2 (c)[0]); \
	(a)[1] op1 ((b)[1] op2 (c)[1]); \
	(a)[2] op1 ((b)[2] op2 (c)[2])
#endif

#ifndef VEC3_CROSS
#define VEC3_CROSS(rr,aa,bb) \
	(rr)[0]= (aa)[1]*(bb)[2] - (aa)[2]*(bb)[1]; \
	(rr)[1]= (aa)[2]*(bb)[0] - (aa)[0]*(bb)[2]; \
	(rr)[2]= (aa)[0]*(bb)[1] - (aa)[1]*(bb)[0]
#endif

#ifndef V3dotV3
#define V3dotV3(a,b) \
	(a)[0]*(b)[0] + (a)[1]*(b)[1] + (a)[2]*(b)[2]
#endif

// #endif: _T2AUTILITIES_H
#endif
